package com.hc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hc.bean.UserBean;
import com.hc.dao.RunSqlDao;
import com.hc.dao.UserDao;
import com.hc.daoImpl.RunSqlDaoImpl;
import com.hc.daoImpl.UserDaoImpl;
import com.hc.service.UserService;
import com.hc.serviceImpl.UserServiceImpl;

public class UserServlet extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		
		String types = req.getParameter("types");  
		
		System.out.println(types);
		if("login".equals(types)){
	
			login(req,resp);
			
		}else if("success".equals(types)){
			HttpSession session = req.getSession();  
			if(session.getAttribute("userName")!=null&&!session.getAttribute("userName").equals("")){
				req.getRequestDispatcher("/main.jsp").forward(req,resp);
			}
		}else if("getUsers".equals(types)){
			
			getUsers(req,resp);
		}else if("delUser".equals(types)){
			
			
			delUser(req,resp);
			
			
		}else if("updateUser".equals(types)){
			
			
			updateUser(req,resp);
			
		}else if("addUser".equals(types)){
			
			addUser(req,resp);
		}else if("getUser".equals(types)){
			
			getUser(req,resp);
		}
	
		
	}

	private void getUser(HttpServletRequest req, HttpServletResponse resp) {
		
		
		HttpSession session = req.getSession();  
		String userName =(String) session.getAttribute("userName");
		
		if(userName!=null&&!"".equals(userName)){
			JSONObject json = new JSONObject();
			json.put("name", userName);
			returnMsg(req,resp, json.toString());
		}
		
		
	}

	private void addUser(HttpServletRequest req, HttpServletResponse resp) {
		if(req.getParameter("userNumber")!=null&&!"".equals(req.getParameter("userNumber"))){
			UserService service = new UserServiceImpl();
			String msg = "false";
			if(service.addUser(req.getParameter("userNumber"), req.getParameter("pwd"), req.getParameter("remark"))){
				 msg = "true";
			}
			returnMsg(req,resp, msg);
		}
		
	}

	private void updateUser(HttpServletRequest req, HttpServletResponse resp) {
		
		if(req.getParameter("userId")!=null&&!"".equals(req.getParameter("userId"))){
			UserService service = new UserServiceImpl();
			
			UserBean bean = new UserBean();
			bean.setId(Integer.parseInt(req.getParameter("userId")));
			bean.setName(req.getParameter("userNumber"));
			bean.setLastLoginTime(req.getParameter("loginTime"));
			bean.setPwd(req.getParameter("pwd"));
			bean.setRemark(req.getParameter("remark"));
			String msg = "false";
			if(service.updateUserBean(bean)){
				 msg = "true";
			}
			returnMsg(req,resp, msg);
		}
	}

	private void delUser(HttpServletRequest req, HttpServletResponse resp) {
		
	
		
		if(req.getParameter("id")!=null&&!"".equals(req.getParameter("id"))){
			UserService service = new UserServiceImpl();
			
			UserBean bean = new UserBean();
			bean.setId(Integer.parseInt(req.getParameter("id")));
			
			String msg = "false";
			if(service.delUserBean(bean)){
				 msg = "true";
			}
			returnMsg(req,resp, msg);
		
		}
		
			
		
	}

	
	
	private void getUsers(HttpServletRequest req, HttpServletResponse resp) {
		
			UserDao dao = new UserDaoImpl();
		 	List<UserBean> beans = dao.getUsers();
			JSONArray json = JSONArray.fromObject(beans);
			returnMsg(req,resp, json.toString());
		
	}

	private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String name = req.getParameter("nameNumber");  
		String pwd = req.getParameter("pwd");  
	 
		
		JSONObject json = new JSONObject();
		
	
			UserService service = new UserServiceImpl();
			 
			if(service.userLogin(name, pwd)){
				
				HttpSession session = req.getSession();  
				session.setAttribute("userName", name);
				json.put("success","true");
			}else{
				json.put("success","false");
			}
		
	
		
		
			returnMsg(req,resp, json.toString());
		
	}

	private void returnMsg(HttpServletRequest req, HttpServletResponse resp,
			String msg) {
		resp.setCharacterEncoding("utf-8");

		PrintWriter w;
		try {
			w = resp.getWriter();
			w.write(msg);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}
	
	
	
	
	
	
}
